<%--
  Created by IntelliJ IDEA.
  User: WHSXT
  Date: 2021/6/17
  Time: 14:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css"/>
</head>
<body class="pear-container">

<%--添加权限的模板--%>
<script type="text/html" id="addPermsTpl">
    <form class="layui-form" action="" onsubmit="return false">
        <div class="mainBox">
            <div class="layui-form-item">
                <label class="layui-form-label">父菜单</label>
                <div class="layui-input-block">
                    <ul id="demoTree" class="dtree" data-id="0">--请选择父菜单--</ul>
                    <input id="parentId" name="parentId" value="0" hidden>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" placeholder="请输入权限的名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限标识</label>
                <div class="layui-input-block">
                    <input type="text" name="tag" placeholder="请输入权限的标识" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限地址</label>
                <div class="layui-input-block">
                    <input type="text" name="href" placeholder="请输入权限地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否展开</label>
                <div class="layui-input-block">
                    <input type="radio" name="spread" value="1" title="展开">
                    <input type="radio" name="spread" value="0" title="关闭" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限类型</label>
                <div class="layui-input-block">
                    <select name="type" lay-verify="required" lay-search>
                        <option value="0" selected>目录</option>
                        <option value="1">菜单</option>
                        <option value="2">按钮</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input type="text" name="icon" placeholder="请输入图标" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="sort" placeholder="请输入排序号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                            lay-filter="perms-save">
                        <i class="layui-icon layui-icon-ok"></i>
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-sm">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</script>
<%--修改权限的模板--%>
<script type="text/html" id="updatePermsTpl">
    <form class="layui-form" action="" onsubmit="return false" lay-filter="update-perms">
        <div class="mainBox">
            <div class="layui-form-item">
                <input name="id" hidden>
                <label class="layui-form-label">父菜单</label>
                <div class="layui-input-block">
                    <select name="parentId" lay-verify="required" lay-search readonly="true" disabled>
                        <option value="0">ROOT</option>
                        <option value="1">工作空间</option>
                        <option value="2">系统管理</option>
                        <option value="3">租赁管理</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" placeholder="请输入权限的名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限标识</label>
                <div class="layui-input-block">
                    <input type="text" name="tag" placeholder="请输入权限的标识" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限地址</label>
                <div class="layui-input-block">
                    <input type="text" name="href" placeholder="请输入权限地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否展开</label>
                <div class="layui-input-block">
                    <input type="radio" name="spread" value="1" title="展开">
                    <input type="radio" name="spread" value="0" title="关闭" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限类型</label>
                <div class="layui-input-block">
                    <select name="type" lay-verify="required" lay-search readonly="true" disabled>
                        <option value="0" selected>目录</option>
                        <option value="1">菜单</option>
                        <option value="2">按钮</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input type="text" name="icon" placeholder="请输入图标" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="sort" placeholder="请输入排序号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                            lay-filter="perms-save">
                        <i class="layui-icon layui-icon-ok"></i>
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-sm">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</script>

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="power-type">
    {{#if (d.type == 0) { }}
    <span>目录</span>
    {{# }else if(d.type == 1){ }}
    <span>菜单</span>
    {{# }else if(d.type == 2){ }}
    <span>按钮</span>
    {{# } }}
</script>

<script type="text/html" id="power-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="展开|关闭" lay-filter="user-enable"
           {{ d.spread == 1 ? 'checked' : '' }}">
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>

<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'dropdown','dtree'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let dropdown = layui.dropdown;
        let dtree = layui.dtree;

        let MODULE_PATH = "operate/";

        window.render = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                // treeIdName : 树节点的id 是我们后端对象里面的那个属性
                treeIdName: 'id',
                // treePidName: 树节点的父亲id 对应我们后端对象的那个属性
                treePidName: 'parentId',
                // 皮肤
                skin: 'line',
                // 请求的方法
                method: 'post',
                // 树默认关闭
                treeDefaultClose: true,
                // 标题
                toolbar: '#power-toolbar',
                elem: '#power-table',
                // 查询的数据不是一个树型,而是一个普通的list 集合
                url: '${pageContext.request.contextPath}/sysperms/findByPage.do',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'title', minWidth: 200, title: '权限名称'},
                        {field: 'tag', minWidth: 200, title: '权限标识'},
                        {field: 'icon', title: '图标', templet: '#icon'},
                        {field: 'type', title: '权限类型', templet: '#power-type'},
                        {field: 'spread', title: '是否展开', templet: '#power-enable'},
                        {field: 'sort', title: '排序'},
                        {title: '操作', templet: '#power-bar', width: 150, align: 'center'}
                    ]
                ],
                parseData: function (res) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "data": res.data //解析数据列表
                    }
                },
                response: {
                    statusCode: 200
                }
            });
        }

        render();

        table.on('tool(power-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })


        table.on('toolbar(power-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'expandAll') {
                treetable.expandAll("#power-table");
            } else if (obj.event === 'foldAll') {
                treetable.foldAll("#power-table");
            }
        });

        window.add = function () {
            layer.open({
                type: 1,
                title: '新增',
                shade: 0.1,
                area: ['600px', '600px'],
                content: $("#addPermsTpl").html(),
                success: function (layerno, index) {
                    form.render();

                    // 给提交按钮绑定事件
                    form.on('submit(perms-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/sysperms/add.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        layer.close(index);//关闭当前页
                                        table.reload("user-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                    // 新增的弹窗里面父菜单的展示
                    let treeConf = {
                        // 将该树放在那个容器里面
                        elem: "#demoTree",
                        // 代表数据 数据可以不是一个树的结构,list 集合就可以了
                        url: "${pageContext.request.contextPath}/sysperms/findByPage.do",
                        width: "80%", // 指定树的宽度
                        dataStyle: "layuiStyle",  //使用layui风格的数据格式
                        response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
                        // 响应给我们的数据为list 集合
                        dataFormat: "list" ,
                        // 我们需要在树里面获取这2 个值
                        selectInputName: {
                            nodeId: "nodeId",
                            context: "context"
                        },
                        checkbarType: "self",
                    };
                    dtree.renderSelect(treeConf);
                    dtree.on('node("demoTree")', function(obj){
                        var param = dtree.selectVal("demoTree");
                        $("#parentId").val(param.nodeId) ;
                    });
                }
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 1,
                title: '修改',
                shade: 0.1,
                area: ['600px', '600px'],
                content: $("#updatePermsTpl").html(),
                success: function (layerno, index) {
                    // 1 回显数据
                    form.val("update-perms", obj.data)
                    // 2 重新渲染
                    form.render();
                    // 3 给按钮绑定事件
                    // 给提交按钮绑定事件
                    form.on('submit(perms-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/sysperms/update.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        layer.close(index);//关闭当前页
                                        table.reload("user-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该权限', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/sysperms/delete.do/" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }


        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些权限', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/sysperms/delete.do/" + ids,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('power-table');
                                form.render();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>
